package com.taptech.doufu.ui.view.topicview;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewAlbumsActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.activity.EasyBrowseActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.DFHomeNovelContentBeans;
import com.taptech.doufu.bean.cp.ResponseCpIndexDataBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;

import org.json.JSONArray;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class NovelTopicImageView extends LinearLayout{
    private DFHomeNovelBeans imageModel;
    private String title;
    public NovelTopicImageView(Context context) {
        super(context);
    }

    public NovelTopicImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NovelTopicImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private Context context;
    private View lay;
    private ImageView ivPicBg;

    public NovelTopicImageView(final Context context, final DFHomeNovelContentBeans model, final String type,String pageType){
        this(context);
        if (model != null && model.getList() != null && model.getList().size() > 0 &&
                model.getList().get(0) != null) {
            View view = LayoutInflater.from(context).inflate(R.layout.view_booklist_item, null);
            this.context = context;
            lay = view.findViewById(R.id.lay);
            ivPicBg = (ImageView) view.findViewById(R.id.ivPicBg);
            ivPicBg.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    UmengEventUtil.addContentEvent(context,model.getEventData(),type,pageType);
                    jump();
                }
            });
            changeDayModelBg();
            setImageHeight();
            imageModel = model.getList().get(0);
            title = model.getTitle();
            updateData();

            int width = ScreenUtil.getScreenWidth(context);
            int height = (int) (width/4.0f);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
            params.gravity = Gravity.CENTER;
            addView(view,params);
        }
    }

    private void setImageHeight() {
        int width =ScreenUtil.getScreenWidth(context);
        int height = (int) (width/4.0f);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
        ivPicBg.setLayoutParams(params);
    }

    private void jump() {
        if (imageModel == null) {
            return;
        }
        Intent intent = new Intent();
        switch (imageModel.getBanner_type()){
            case Constant.MainHomeImageCardType.JUMP_TYPE_OUTSIDE_WEBVIEW:
                intent.setClass(context, EasyBrowseActivity.class);
                intent.putExtra(Constant.URL, imageModel.getBanner_link());
                intent.putExtra(Constant.TITLE, title);
                context.startActivity(intent);
                break;
            case Constant.MainHomeImageCardType.JUMP_TYPE_INSIDE_WEBVIEW:
                intent.setClass(context, BrowseActivity.class);
                intent.putExtra(Constant.URL, imageModel.getBanner_link());
                intent.putExtra(Constant.TITLE, title);
                context.startActivity(intent);
                break;
            case Constant.MainHomeImageCardType.JUMP_ALBUM:
                intent.setClass(context, NewAlbumsActivity.class);
                intent.putExtra(Constant.URL, imageModel.getBanner_link());
                intent.putExtra(Constant.NAME, title);
                context.startActivity(intent);
                break;
            case Constant.MainHomeImageCardType.JUMP_TYPE_WEEX:
                SimpleWeexActivity.startActivity(context,imageModel.getBanner_link());
                break;
            default:
                SimpleWeexActivity.startActivity(context,imageModel.getBanner_link());
                break;
        }
    }

    private void updateData() {
        if (imageModel != null) {
            GlideUtil.displayRoundImage(context, ivPicBg, imageModel.getBanner_img(), 3);
        }
    }

    public void refrush(ResponseCpIndexDataBean.DataBean.ListBean model) {

        try {
            JSONArray jsonArray = new JSONArray(model.getList().toString());
            this.imageModel = DiaobaoUtil.json2list(DFHomeNovelBeans.class, jsonArray).get(0);
            updateData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        ivPicBg.setAlpha(1.0f);
    }

    private void darkBg() {
        ivPicBg.setAlpha(Constant.imagViewAlpha);
    }
}
